var express = require("express");
var router = express.Router();
var {
  FindOneDataMore,
  InsertManyData,
  UpdateTableData,
  FindTableData,
  RemoveTableData,
} = require("../db");
const { organize_model } = require("../db/model");
var axios = require("axios");
const { getToken, checkToken } = require("../utils/token");
var multer = require("multer"); //上传文件

//组织新增
router.post("/organizeAdd", (req, res) => {
  var body = req.body;
  body.time = new Date();
  checkToken(req, res, ({ phone }) => {
    InsertManyData({
      model: organize_model,
      data: body,
      res,
      msg: "成功",
    });
  });
});
// 组织列表
router.post("/organizeList", (req, res) => {
  var query = {};
  var { keyword } = req.body;
  if (keyword) {
    query = {
      $or: [
        {
          name: new RegExp(keyword), //单位名称
        },
        {
          class: new RegExp(keyword), //部门
        },
        {
          address: new RegExp(keyword), //地址
        },
      ],
    };
  }
  checkToken(req, res, ({ phone }) => {
    FindTableData({
      model: organize_model,
      query: query,
      msg: "成功",
      res,
    });
  });
});
// 组织删除
router.post("/organizeDelete", (req, res) => {
  var body = req.body;
  checkToken(req, res, ({ phone }) => {
    RemoveTableData({
      model: organize_model,
      query: {
        _id: body._id,
      },
      msg: "成功",
      res,
    });
  });
});

// 查询一个组织
router.post("/organizeOne", (req, res) => {
  var body = req.body;
  checkToken(req, res, ({ phone }) => {
    FindOneDataMore({
      model: organize_model,
      query: {
        _id: body._id,
      },
      msg: "成功",
      res,
    });
  });
});

// 修改组织
router.post("/organizeUpdate", (req, res) => {
  var body = req.body;
  checkToken(req, res, ({ phone }) => {
    UpdateTableData({
      model: organize_model,
      query: {
        _id: body._id,
      },
      data: body,
      msg: "成功",
      res,
    });
  });
});
module.exports = router;
